package com.qqtech.qquan.user.controller;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.web.config.EnableSpringDataWebSupport;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.common.exception.BizzException;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.push.model.PushAudience;
import com.qqtech.qquan.push.service.PushAudienceService;
import com.qqtech.qquan.push.service.PushBizzService;
import com.qqtech.qquan.user.model.UserLogin;
import com.qqtech.qquan.user.model.UserLoginQuery;
import com.qqtech.qquan.user.model.UserValue;
import com.qqtech.qquan.user.service.UserBaseService;
import com.qqtech.qquan.user.service.UserLoginService;
import com.qqtech.qquan.user.service.UserValueService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/user/login")
@Configuration
@EnableWebMvc
@EnableSpringDataWebSupport
public class UserLoginController extends BaseWebController<UserLogin, UserLoginQuery> {
	@Resource
	private UserLoginService userLoginService;
	@Resource
	private UserValueService userValueService;
	@Resource
	private PushBizzService pushBizzService;
	@Resource
	private UserBaseService userBaseService;
	@Resource
	private PushAudienceService pushAudienceService;

	@Override
	protected BaseService<UserLogin> getBaseService() {
		return userLoginService;
	}

	/**
	 * 修改登录密码
	 * 
	 * @param map
	 * @return
	 * @throws BizzException
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/doChangePwd")
	@ResponseBody
	public Result doChangePwd(@RequestBody Map<String, String> map) throws BizzException {
		return userLoginService.doChangePwdWithVerifycode(super.getUserId(), map.get("password"),
				map.get("verifycode"));
	}

	@RequestMapping(value = "/doLogout")
	@ResponseBody
	public Result doLogout(@RequestBody Map<String, String> map) {
		Integer loginUserId = super.getUserId();
		if (loginUserId != null && loginUserId.intValue() != 0) {
			userValueService.alterRegistrationId(loginUserId, null);
			PushAudience pushAudience = new PushAudience();
			pushAudience.setUserId(loginUserId);
			pushAudienceService.delete(pushAudience);
		}
		return new Result();
	}

	/**
	 * 用户登录-使用验证码登录
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/doLoginWithPhoneCode")
	@ResponseBody
	public Result doLoginWithPhoneCode(@RequestBody Map<String, String> map) {
		Result result = userLoginService.doLoginWithPhoneCode(map.get("phone"), map.get("verifycode"),
				map.get("deviceId"));
		if (result.checkForm()) {
			Integer loginUserId = (Integer) result.get("loginUserId");
			if (loginUserId != null && loginUserId.intValue() != 0) {
				UserValue userValue = userValueService.getById(loginUserId);
				if (userValue != null) {
					String registrationId = map.get("registrationId");
					if (StringUtil.isNotBlank(registrationId)) {
						if (!registrationId.equals(userValue.getRegistrationId())) {
							pushBizzService.userOffWhenLoginOtherDevice(loginUserId, userValue.getRegistrationId());
							userValueService.alterRegistrationId(loginUserId, registrationId);
						}
					}
				}
			}
		}
		return result;
	}

	/**
	 * 用户登录-使用密码登录
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/doLoginWithPhonePwd")
	@ResponseBody
	public Result doLoginWithPhonePwd(@RequestBody Map<String, String> map) {
		Result result = userLoginService.doLoginWithPhonePwd(map.get("phone"), map.get("password"));
		if (result.checkForm()) {
			Integer loginUserId = (Integer) result.get("loginUserId");

			if (loginUserId != null && loginUserId.intValue() != 0) {
				UserValue userValue = userValueService.getById(loginUserId);
				if (userValue != null) {
					String registrationId = map.get("registrationId");
					if (StringUtil.isNotBlank(registrationId)) {
						if (!registrationId.equals(userValue.getRegistrationId())) {
							pushBizzService.userOffWhenLoginOtherDevice(loginUserId, userValue.getRegistrationId());
							userValueService.alterRegistrationId(loginUserId, registrationId);
						}
					}
				}
			}
		}
		return result;
	}

	/**
	 * 注册后，提示用户修改密码和昵称
	 * 
	 * @param map
	 * @return
	 * @throws BizzException
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/initUser")
	@ResponseBody
	public Result initUser(@RequestBody Map<String, String> map) throws BizzException {
		return userLoginService.initUser(super.getUserId(), map.get("password"), map.get("nick"));
	}

	/**
	 * 用户是否通过社区认证
	 * 
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/isCommunityAuth")
	@ResponseBody
	public Result isCommunityAuth() {
		Result result = new Result();
		result.put("isCommunityAuth", userLoginService.isCommunityAuth(super.getUserId()));
		return result;
	}
}
